<?php
	class Admin_log extends My_Controller
	{
		function Admin_log()
		{
			parent::__construct();
			$this->check_login();
			$this->permiss_admin();
		}			
		
		function index()
		{
			$_SESSION['user_admin']	= NULL;
			$data['title']			= 'Quản Lý Nhật Ký Website';
			$this->load->library('pagination');
			$data['admin_admin']	= $this->db->get_where('admins', array())->result_array();
			$per_page 				= 30;
			$join 	= array('admins'=>'admins.id = logs.admin_id');
			$select = 'logs.*,admins.username';						
			$total 					= $this->mod_lib->count('logs',array());	
			$total 	  				= $this->mod_lib->count_search_join('logs',array(),array(),$join);
			$data['admin_log']		= $this->mod_lib->load_all_search_join('logs',$select,array(),array(),$join,$per_page,$this->uri->segment(4),array('time'=>'DESC'));											
			$base_url				= site_url('admins/admin_log/index/');
			$config['base_url']		= $base_url;
			$config['per_page']		= $per_page;
			$config['total_rows']	= $total;
			$config['uri_segment']	= 4;
			$this->pagination->initialize($config); 
			$data['paging']			= $this->pagination->create_links();
			$data['content_admin']	= 'admins/admin_log/index';
			$this->load->view('admins/template_admin', $data);
		}
		
		function delete()
		{ 
			$i = $this->permiss_admin_add();
			if(isset($i['all']) || isset($i['del']))
			{
				if ($this->input->post('cmd'))
				{
					$id		= $this->input->post('chon');
					$id		= substr($id, 0, strlen($id)-1);
					$id		= explode(',', $id);
					$this->mod_lib->delete('logs', $id, 'id');
				}
				else if ($this->input->post('cmdall'))
				{
					$this->db->delete('logs');
				}
				redirect($_SESSION['last_url_admin']);
			}
			else
			{
				$_SESSION['permiss_error'] = "Bạn không có quyền xóa nhật ký website";
				redirect(base_url().'admins/admin_log');
			}
		}
		
		function search()
		{
			if ($this->input->post('cmd'))
			{
				$where	 		= array();
				$user_admin 	= $this->input->post('user_admin');
				if (!empty($user_admin))
				{
					$data['admin_id'] = $user_admin;
					$where 	= array('logs.admin_id'=>$user_admin);
				}
				$_SESSION['search_log'] = array('where'=>$where);
				$_SESSION['user_admin']	= $user_admin;
			}
			// Not submit form search
			$join 					= array('admins'=>'admins.id = logs.admin_id');
			$select 				= 'logs.*, admins.username';
			$this->load->library('pagination');
			$data['title'] 	 		= 'Kết Quả Tìm Kiếm';								
			$per_page 				= 30;
			$total 	  				= $this->mod_lib->count_search_join('logs', array(), $_SESSION['search_log']['where'], $join);
			if ($this->uri->segment(4) == null)
			{			
				$offset = 0;	
			}
			else
			{
				$offset = $this->uri->segment(4);	 
			}	
			$data['admin_admin']	= $this->db->get_where('admins', array())->result_array();
			$data['admin_log']		= $this->mod_lib->load_all_search_join('logs', $select, $_SESSION['search_log']['where'], array(), $join, $per_page, $this->uri->segment(4), array('time'=>'DESC'));	
			$base_url				= site_url('admins/admin_log/search');
			$config['base_url']		= $base_url;
			$config['per_page']		= $per_page;
			$config['total_rows']	= $total;
			$config['uri_segment']	= 4;
			$data['rs']				= $this->uri->segment(4);
			$data['uri']			= $this->uri->uri_string();
			$this->pagination->initialize($config); 
			$data['paging']			= $this->pagination->create_links();							
			$data['content_admin']	= 'admins/admin_log/index';			
			$this->load->view('admins/template_admin', $data);	
		}
	}